package com.sywb.chuangyebao.view;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.contract.ListCheckContract;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;
import org.bining.footstone.utils.ToastUtils;
import org.bining.footstone.widget.PullToRefreshView;

import java.util.LinkedHashMap;

import butterknife.BindView;

/**
 * 列表展示 选择
 */

public class ListCheckActivity extends ActionbarActivity<ListCheckContract.Presenter> implements ListCheckContract.ListCheckView {

    @BindView(R.id.common_recycler)
    RecyclerView commonRecycler;
    @BindView(R.id.common_refresh)
    PullToRefreshView commonRefresh;
    @BindView(R.id.common_nodata_content)
    TextView commonNodataContent;
    @BindView(R.id.common_nodata_icon)
    ImageView commonNodataIcon;
    @BindView(R.id.common_nodata_subtitle)
    TextView commonNodataSubtitle;
    @BindView(R.id.common_nodata)
    RelativeLayout commonNodata;

    private String title = "我的收藏";
    private String oldName;

    private LinkedHashMap<Integer, String> users;

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.layout_recycler_refresh;
    }

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        if (bundle == null) {
            title = getIntent().getStringExtra("p0");
            oldName = getIntent().getStringExtra("p1");
        } else {
            title = bundle.getString("p0");
            oldName = bundle.getString("p1");
        }
        setTitle(title);
    }

    private TextView menu;

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
        switch (title) {
            case "兴趣行业":
                mPresenter.initGoodAtView();
                addMenuTextItme(R.string.save, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.saveGoodArea();
                    }
                });
                break;
            case "我的收藏":
                mPresenter.initCollectionView();
                break;
            case "我的提问":
                mPresenter.initQuestionView();
                break;
            case "意见反馈":
                addMenuTextItme(R.string.add, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        advance(UserOpinionActivity.class, "意见反馈");
                    }
                });
                mPresenter.initFeedBackView();
                break;
            case "向谁提问":
                menu = addMenuTextItme(R.string.btn_confirm, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (users != null && users.size() > 0) {
                            RxBus.get().post(Constants.POST_LIST_TUTOR, users != null && users.size() > 0 ? JSON.toJSONString(users) : "");
                            finish();
                            overridePendingTransition(0, R.anim.view_out_to_right);
                        } else {
                            ToastUtils.show(mActivity, R.string.ask_who);
                        }
                    }
                });
                Logger.e("@中：" + oldName);
                mPresenter.initAskView(oldName);
                break;
            case "培训视频":
                mPresenter.initVideoView();
                break;
            case "直播":
                mPresenter.initLiveView();
                break;
            case "培训音频":
                mPresenter.initAudioView();
                break;
            case "专题栏目":
                mPresenter.initSpecialView();
                break;
        }
    }

    @Override
    public void exit() {
        if (title.equals("向谁提问")) {
            RxBus.get().post(Constants.POST_LIST_TUTOR, "");
        }
        super.exit();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("p0", title);
        outState.putString("p1", oldName);
        super.onSaveInstanceState(outState);
    }

    @Override
    public String getStatisticsIdentity() {
        return title;
    }

    @Override
    public PullToRefreshView getPullToRefreshView() {
        return commonRefresh;
    }

    @Override
    public RecyclerView getRecyclerView() {
        return commonRecycler;
    }

    @Override
    public void setNoData(boolean isNoData) {
        commonNodata.setVisibility(isNoData ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setAskConfirmText(LinkedHashMap<Integer, String> checkedSize) {
        this.users = checkedSize;
        if (checkedSize.size() > 0) {
            menu.setText("确定(" + checkedSize.size() + "/5)");
        } else {
            menu.setText(R.string.confirm_btn);
        }
    }

    @Override
    public boolean useRxBus() {
        return true;
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_SAVE_QUESTION)})
    public void rxAfterCollected(String data) {
        Logger.e("操作了收藏" + data);

        if (!"我的提问".equals(title)) {
            return;
        }

        mPresenter.initQuestionView();
    }

}
